﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Net.Sz.Framework.ExcelTools.CreateCode.excel.java
{
    /// <summary>
    ///
    /// <para>PS:</para>
    /// <para>@author 失足程序员</para>
    /// <para>@Blog http://www.cnblogs.com/shizuchengxuyuan/</para>
    /// <para>@mail 492794628@qq.com</para>
    /// <para>@phone 13882122019</para>
    /// </summary>
    public class CreateJavaMyJpaCode : CreateBase
    {
        static readonly CreateJavaMyJpaCode instance = new CreateJavaMyJpaCode();
        public static CreateJavaMyJpaCode Instance()
        {
            return instance;
        }

        protected override void CreateClassFile(string fileName, string msg)
        {
            base.CreateClassFile(JavaJpaCodePathData + "/" + fileName + ".java", msg);
        }

        public void ActionExcelData(ExcelDatas exceldatas, string cellgs, string strnamespace, Action<string> show)
        {
            Dictionary<string, ExcelData> datas = exceldatas.Datas;

            List<string> snames = new List<string>();

            HashSet<string> filenames = new HashSet<string>();

            foreach (var data in datas)
            {
                string filename = ExcelData.FirstUpper(data.Value.SheetName.Replace("_", ""));

                if (data.Value.Rows == null || data.Value.Rows.Count == 0 || !filenames.Add(filename))
                {
                    continue;
                }
                CreateClassFile("Q" + filename, CreateCode(datas, data.Value, cellgs, strnamespace, show));
            }
        }

        string CreateCode(Dictionary<string, ExcelData> datas, ExcelData model, string cellgs, string strnamespace, Action<string> show)
        {
            StringBuilder csharpBuilder = new StringBuilder();
            StringBuilder csharpBuilder2 = new StringBuilder();
            string sheetName = model.SheetName;

            string className = "Q" + ExcelData.FirstUpper(sheetName.Replace("_", ""));

            csharpBuilder.Append("package ").Append(strnamespace).AppendLine(";")
                .AppendLine()
                .AppendLine()
                .AppendLine("import com.ty.tools.db.struct.DbColumn;")
                .AppendLine("import com.ty.tools.db.struct.DbTable;")
                .AppendLine("import com.ty.tools.db.struct.DataBaseModel;")
                .AppendLine()
                .AppendLine("import java.io.Serializable;")
                .AppendLine()
                .AppendLine("/**")
                .AppendLine(" * ")
                .AppendLine(" * @author: Troy.Chen(失足程序员 ， 15388152619)")
                .AppendLine(" * @create: 2020-12-30 20:33")
                .AppendLine(" */")
                .AppendLine("@DbTable(mappedSuperclass = true, name = \"" + sheetName + "" + "\", desc = \"Excel File = " + model.FileName + "\")")
                .AppendLine("public abstract class " + className + " extends DataBaseModel implements Serializable {")
                .AppendLine();

            foreach (var row in model.Rows)
            {
                var cells = row.Value.Cells;
                foreach (var cell in cells)
                {
                    if (cell.Value.Cells.Equals("ALL") || cellgs.Equals("ALL") || cellgs.Equals(cell.Value.Cells))
                    {

                        csharpBuilder.Append("    /**").AppendLine();
                        csharpBuilder.Append("     * " + cell.Value.CellNotes).Append((String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : " 关联文件：" + cell.Value.CellFileName)).AppendLine();
                        csharpBuilder.Append("     */").AppendLine();
                        csharpBuilder.Append("    @DbColumn(name = \"").Append(cell.Value.CellName).Append("\"").Append(cell.Value.IsPKey ? ", key = true" : "").AppendLine(")");
                        csharpBuilder.Append("    protected " + cell.Value.CellValueType + " " + cell.Value.CellName2Coder + ";").AppendLine();

                        csharpBuilder2
                            .AppendLine()
                            .AppendLine("    /**")
                            .AppendLine("     * " + cell.Value.CellNotes + (String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : (" 关联文件：" + cell.Value.CellFileName) + "<br>"))
                            .AppendLine("     */")
                            .AppendLine("    public " + cell.Value.CellValueType + " get" + cell.Value.CellName2CoderUpper + "() {")
                            .AppendLine("        return this." + cell.Value.CellName2Coder + ";")
                            .AppendLine("    }")
                            .AppendLine()
                            .AppendLine("    /**")
                            .AppendLine("     * " + cell.Value.CellNotes + (String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : (" 关联文件：" + cell.Value.CellFileName) + "<br>"))
                            .AppendLine("     */")
                            .AppendLine("    public " + className + " set" + cell.Value.CellName2CoderUpper + "(" + cell.Value.CellValueType + " " + cell.Value.CellName2Coder + ") {")
                            .AppendLine("        this." + cell.Value.CellName2Coder + " = " + cell.Value.CellName2Coder + ";")
                            .AppendLine("        return this;")
                            .AppendLine("    }");
                    }
                }
                break;
            }
            csharpBuilder.Append(csharpBuilder2);
            csharpBuilder.AppendLine("}");// Class End
            return csharpBuilder.ToString();
        }
    }
}

